package com.greenantmall.utils;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVInstallation;
import com.avos.avoscloud.AVPush;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.PushService;
import com.avos.avoscloud.SendCallback;
import com.greenantmall.activity.MyInfoActivity;
import com.greenantmall.activity.MyOrdersActivity;
import com.greenantmall.broadcast.MallManagerReceiver;

public class NotificationUtil {
	/**
	 * 发送给特定设备通知
	 * 
	 * @param installId
	 * @since 3.8
	 */
	public static void sendMsg(String installId) {
		AVQuery pushQuery = AVInstallation.getQuery();
		// 假设 THE_INSTALLATION_ID 是保存在用户表里的 installationId，
		// 可以在应用启动的时候获取并保存到用户表
		pushQuery.whereEqualTo("installationId", installId);
		AVPush.sendMessageInBackground("message to installation", pushQuery, new SendCallback() {
			@Override
			public void done(AVException e) {

			}
		});
	}

	public static void sendPrivateMsg(String msg, String msgTypeId, int msgType) {
		AVPush push = new AVPush();

		try {
			String userId = "";
			AVUser user = AVUser.getCurrentUser();
			if (user != null) {
				userId = user.getObjectId();
			}

			JSONObject data = new JSONObject("{\"action\": \"" + MallManagerReceiver.ACTION + "\", " + "\"msg\": \""
					+ msg + "\"," + "\"time\": " + System.currentTimeMillis() + "," + "\"msgTypeId\": \"" + msgTypeId
					+ "\"," + "\"msgType\": " + msgType + "," + "\"userId\": \"" + userId + "\"" + "}");

			push.setData(data);
			String installationId = AVInstallation.getCurrentInstallation().getInstallationId();
			push.setCloudQuery("select * from _Installation where installationId ='" + installationId + "'");
			push.sendInBackground(new SendCallback() {

				@Override
				public void done(AVException e) {

				}
			});
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 發送到店鋪端
	 * 
	 * @param msg
	 * @param msgTypeId
	 * @param msgType
	 * @param installId
	 * @param storeId
	 */
	public static void sendMsgToStore(String msg, String msgTypeId, int msgType, String installId, String storeId) {
		AVPush push = new AVPush();

		try {
			if (TextUtils.isEmpty(installId) || TextUtils.isEmpty(msg)) {
				return;
			}
			String userId = "";
			AVUser user = AVUser.getCurrentUser();
			if (user != null) {
				userId = user.getObjectId();
			}

			JSONObject data = new JSONObject("{\"action\": \"" + MallManagerReceiver.STORE_ACTION + "\", "
					+ "\"msg\": \"" + msg + "\"," + "\"time\": " + System.currentTimeMillis() + ","
					+ "\"msgTypeId\": \"" + msgTypeId + "\"," + "\"msgType\": " + msgType + "," + "\"userId\": \""
					+ userId + "\"," + "\"storeId\": \"" + storeId + "\"" + "}");
			push.setData(data);
			push.setCloudQuery("select * from _Installation where installationId ='" + installId + "'");
			push.sendInBackground(new SendCallback() {

				@Override
				public void done(AVException e) {

				}
			});
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * 注册消息接收
	 * 
	 * @param context
	 * @since 3.8
	 */
	public static void registerNotificationReceiver(Context context) {
		PushService.setDefaultPushCallback(context, MyInfoActivity.class);
		if (AVUser.getCurrentUser() != null) {
			PushService.subscribe(context, "Order_" + AVUser.getCurrentUser().getObjectId(), MyOrdersActivity.class);

		}
		PushService.subscribe(context, "public", MyInfoActivity.class);
	}

	/**
	 * 取消注册消息接收
	 * 
	 * @param context
	 * @since 3.8
	 */
	public static void unregisterNotificationReceiver(Context context) {
		PushService.setDefaultPushCallback(context, MyInfoActivity.class);
		unRegisterNotificationWhenUserChange(context);
		PushService.unsubscribe(context, "public");
		// 退订之后需要重新保存 Installation
		AVInstallation.getCurrentInstallation().saveInBackground();
	}

	/**
	 * 切换用户之前取消注册消息接收
	 * 
	 * @param context
	 * @since 3.8
	 */
	public static void unRegisterNotificationWhenUserChange(Context context) {
		if (AVUser.getCurrentUser() != null) {
			PushService.unsubscribe(context, "Order_" + AVUser.getCurrentUser().getObjectId());

		}
	}
}
